package com.library.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式匹配工具类
 */
public class RegexUtils {
	/**
	 * 判断是否是邮箱
	 * @param valueStr
	 * @return
	 */
	public static boolean isEmail(String valueStr){
		String reg="\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		return  match(reg,valueStr);
	}

	/**
	 * 判断是否是图片的url
	 *
	 * @param valueStr
	 * @return
	 */
	public static boolean isImgUrl(String valueStr){
		String reg=".*?(gif|jpeg|png|jpg|bmp)";
		return  match(reg,valueStr);
	}

	/**
	 * 判断是否是web url
	 * @param valueStr
	 * @return
	 */
	public static boolean isWebUrl(String valueStr)
	{
		String reg="^(https|http)://.*?$(net|com|.com.cn|org|me|)";
		return  match(reg,valueStr);
	}
	/**
	 * 判断字符串是否全部是数字组成
	 * @param str
	 * @return
	 */
	public static boolean isNumStr(String str) {
		Pattern p = Pattern.compile("^\\d+$");
		Matcher m = p.matcher(str);
		return m.matches();
	}

	/**
	 * 判断该字符串是否是ip字符串
	 * @param ip
	 * @return
	 */
	public static boolean isIpNumStr(String ip) {
		Pattern pattern = Pattern
				.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
		Matcher matcher = pattern.matcher(ip);
		return matcher.matches();
	}



	/**
	 * 校验是否是手机号
	 * @param phone
	 * @return
	 */
	public static boolean isPhoneNo(String phone){
		String reg="^((13[0-9])|(15[^4,\\D])|(17[0-9])|(18[0-9]))\\d{8}";
		return match(reg,phone);
	}

	/**
	 * 获取一个字符串中的所有的数字
	 * @param str
	 * @return abc123de45ef56--->1234556
     */
	public static String getNumStr(String str){
		String reg="[^0-9]";
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(str);
		return m.replaceAll("").trim();
	}

	public static boolean match(String reg,String content){
		Pattern pattern = Pattern
				.compile(reg);
		Matcher matcher = pattern.matcher(content);
		return matcher.matches();
	}
}
